using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;

namespace JingQiao.Ads.Infrastructure.Data.DB4O.DomainObject
{
    using Domain.Model.DomainObject;

    public partial class Position : IPosition
    {
        #region Primitive Properties
    
        public virtual int PositionId
        {
            get;
            set;
        }
    
        public virtual int TemplateId
        {
            get { return _templateId; }
            set
            {
                if (_templateId != value)
                {
                    if (Template != null && Template.TemplateId != value)
                    {
                        Template = null;
                    }
                    _templateId = value;
                }
            }
        }
        private int _templateId;
    
        public virtual string UniqueCode
        {
            get;
            set;
        }
    
        public virtual string Name
        {
            get;
            set;
        }
    
        public virtual string Summary
        {
            get;
            set;
        }
    
        public virtual byte AdType
        {
            get;
            set;
        }
    
        public virtual int Num
        {
            get;
            set;
        }
    
        public virtual int Width
        {
            get;
            set;
        }
    
        public virtual int Height
        {
            get;
            set;
        }
    
        public virtual Nullable<int> Length
        {
            get;
            set;
        }
    
        public virtual byte Status
        {
            get;
            set;
        }
    
        public virtual int Sequence
        {
            get;
            set;
        }
    
        public virtual string Creator
        {
            get;
            set;
        }
    
        public virtual System.DateTime CreateDate
        {
            get;
            set;
        }

        #endregion
        #region Navigation Properties
    
        public virtual ICollection<IDestPositionAd> DestPositionAd
        {
            get
            {
                if (_destPositionAd == null)
                {
                    var newCollection = new FixupCollection<IDestPositionAd>();
                    newCollection.CollectionChanged += FixupDestPositionAd;
                    _destPositionAd = newCollection;
                }
                return _destPositionAd;
            }
            set
            {
                if (!ReferenceEquals(_destPositionAd, value))
                {
                    var previousValue = _destPositionAd as FixupCollection<IDestPositionAd>;
                    if (previousValue != null)
                    {
                        previousValue.CollectionChanged -= FixupDestPositionAd;
                    }
                    _destPositionAd = value;
                    var newValue = value as FixupCollection<IDestPositionAd>;
                    if (newValue != null)
                    {
                        newValue.CollectionChanged += FixupDestPositionAd;
                    }
                }
            }
        }
        private ICollection<IDestPositionAd> _destPositionAd;
    
        public virtual ITemplate Template
        {
            get { return _template; }
            set
            {
                if (!ReferenceEquals(_template, value))
                {
                    var previousValue = _template;
                    _template = value;
                    FixupTemplate(previousValue);
                }
            }
        }
        private ITemplate _template;

        #endregion
        #region Association Fixup
    
        private void FixupTemplate(ITemplate previousValue)
        {
            if (previousValue != null && previousValue.Position.Contains(this))
            {
                previousValue.Position.Remove(this);
            }
    
            if (Template != null)
            {
                if (!Template.Position.Contains(this))
                {
                    Template.Position.Add(this);
                }
                if (TemplateId != Template.TemplateId)
                {
                    TemplateId = Template.TemplateId;
                }
            }
        }
    
        private void FixupDestPositionAd(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems != null)
            {
                foreach (IDestPositionAd item in e.NewItems)
                {
                    item.Position = this;
                }
            }
    
            if (e.OldItems != null)
            {
                foreach (IDestPositionAd item in e.OldItems)
                {
                    if (ReferenceEquals(item.Position, this))
                    {
                        item.Position = null;
                    }
                }
            }
        }

        #endregion
    }
}
